package cn.pfz.controller;


import cn.pfz.Service.ISystemDictionaryItemSerives;
import cn.pfz.domain.JsonResult;
import cn.pfz.domain.SystemDictionary;
import cn.pfz.domain.SystemDictionaryItem;
import cn.pfz.query.QueryObject;
import cn.pfz.query.SystemDictionaryItemQueryObject;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemSerives systemDictionaryItemSerives;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemDictionaryItemQueryObject qo) {

        Subject subject = SecurityUtils.getSubject();
        if(subject.isPermitted("systemDictionaryItem:list")){
            //用于显示主分类
            List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemSerives.selectAll();
            model.addAttribute("systemDictionaryList",systemDictionaryItems);

            List<SystemDictionary> Parent = systemDictionaryItemSerives.selectParent();
            model.addAttribute("parents",Parent);

            PageInfo<SystemDictionaryItem> list = systemDictionaryItemSerives.list(qo);
            model.addAttribute("result",list);
            model.addAttribute("qo",qo);
            return "systemDictionaryItem/list";
        }
        return "redirect:/erro/nopermission";

    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem department){
        if(department.getId()!=null){
            //修改
            systemDictionaryItemSerives.updateByPrimaryKey(department);
        }else{
            systemDictionaryItemSerives.insert(department);
        }
        return "redirect:/systemDictionaryItem/list";
    }

    @ResponseBody
    @RequestMapping ("/delete")
    public JsonResult delete(Long id){
        systemDictionaryItemSerives.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }

}
